<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:member:remove']"
        >删除</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="el-form-search">
                        <el-form-item label="" prop="mobile" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.mobile"
                                    placeholder="请输入手机号码"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="" prop="nickname" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.nickname"
                                    placeholder="请输入用户昵称，姓名"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="" prop="department" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.department"
                                    placeholder="请输入部门或者公司或者行业"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>

            <el-form-item class="el-form-search-item">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
    </el-row>

    <el-table ref="list" :height="tableHeight" v-loading="loading" :data="memberList" @selection-change="handleSelectionChange" @row-click="handleRowClick" highlight-current-row>
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="#" type="index" width="50" align="center">
            <template scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
            </template>
        </el-table-column>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="用户类型" align="center" prop="type" />
      <el-table-column label="手机号码" align="center" prop="mobile" />

      <el-table-column label="用户状态" align="center" prop="status" />
      <el-table-column label="用户昵称，姓名" align="center" prop="nickname" />
      <el-table-column label="职位" align="center" prop="position" />
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="渠道名称" align="center" prop="channelName" />

      <el-table-column label="房东的公司名称" align="center" prop="companyName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:member:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="用户昵称，姓名" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入用户昵称，姓名" />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入部门或者公司或者行业，直接用字典的中文，对于内部人员就是部门，对于中介，就是中介公司，对于渠道就是行业" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>

        <el-form-item label="渠道签约的日期" prop="channelSignDate">
          <el-date-picker clearable size="mini"
            v-model="form.channelSignDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择渠道签约的日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="房东的物业名称" prop="propertyName">
          <el-input v-model="form.propertyName" placeholder="请输入房东的物业名称" />
        </el-form-item>
        <el-form-item label="房东的公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入房东的公司名称" />
        </el-form-item>
        <el-form-item label="营业执照编号" prop="businessLicenseNo">
          <el-input v-model="form.businessLicenseNo" placeholder="请输入营业执照编号" />
        </el-form-item>
        <el-form-item label="工号" prop="employeeNumber">
          <el-input v-model="form.employeeNumber" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="微信收款二维码" prop="paymentQrImgUrl">
          <el-input v-model="form.paymentQrImgUrl" placeholder="请输入微信收款二维码图片url" />
        </el-form-item>
        <el-form-item label="微信好友二维码" prop="weixinQrImgUrl">
          <el-input v-model="form.weixinQrImgUrl" placeholder="请输入微信好友二维码图片url" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember } from "@/api/system/member";

export default {
  name: "Member",
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员信息表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "create_time",
        isAsc: "desc",
        type: null,
        mobile: null,
        spreader: null,
        spreaderBindTime: null,
        status: null,
        avatar: null,
        gender: null,
        nickname: null,
        position: null,
        department: null,
        channelName: null,
        address: null,
        channelMaster: null,
        channelDockMember: null,
        channelSource: null,
        channelSignDate: null,
        channelCooperateUrl: null,
        manager: null,
        propertyName: null,
        companyName: null,
        businessLicenseNo: null,
        miniOpenid: null,
        employeeNumber: null,
        bankName: null,
        bankAccountName: null,
        bankAccountNo: null,
        identifier: null,
        idBack: null,
        idFront: null,
        idStart: null,
        idEnd: null,
        idAddress: null,
        paymentQrImgUrl: null,
        weixinQrImgUrl: null,
        source: null,
        internalType: null,
        landladyType: null,
        pdfPermission: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "用户类型，有中介，内部人员，推广渠道人员，渠道管理人员，租户，房东，根据枚举定义来不能为空", trigger: "change" }
        ],
        mobile: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "用户状态不能为空", trigger: "blur" }
        ],
        internalType: [
          { required: true, message: "内部人员类别,1-业务,2-内勤不能为空", trigger: "change" }
        ],
        landladyType: [
          { required: true, message: "房东类型,0-无意义,1-公司,2-个人不能为空", trigger: "change" }
        ],
        pdfPermission: [
          { required: true, message: "PDF查看权限,1-有权限,0-没有权限,房东角色需要用到不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员信息列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        mobile: null,
        spreader: null,
        spreaderBindTime: null,
        status: "0",
        avatar: null,
        gender: null,
        nickname: null,
        position: null,
        department: null,
        channelName: null,
        address: null,
        channelMaster: null,
        channelDockMember: null,
        channelSource: null,
        channelSignDate: null,
        channelCooperateUrl: null,
        manager: null,
        propertyName: null,
        companyName: null,
        businessLicenseNo: null,
        createTime: null,
        updateTime: null,
        miniOpenid: null,
        employeeNumber: null,
        bankName: null,
        bankAccountName: null,
        bankAccountNo: null,
        identifier: null,
        idBack: null,
        idFront: null,
        idStart: null,
        idEnd: null,
        idAddress: null,
        paymentQrImgUrl: null,
        weixinQrImgUrl: null,
        source: null,
        internalType: null,
        landladyType: null,
        pdfPermission: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 单击行选中数据
    handleRowClick(row, column, event) {
      this.$refs.list.toggleRowSelection(row);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function() {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
